import publicForm from "@/components/public/el-form.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import {
    // getBaseDepartmentUser, //提交审核人
    getWeekdutyDetailByFid, //详情
    saveWeekdutyDetail, //保存修改
    getWeeks, //根据总结日期得到周次
    // getUserCompanyTreeListXm, //巡查项目部
    getfChargeuseridData, // 根据是否待岗查网格信息
} from "@/api/request.js";
export default {
    props: [
        "dialogVisible",
        "dialogTitle",
        "projectId",
        "status",
        "statusNum",
        "dataFid",
        "currentRow",
        "projectName",
        "fCompanyid"
    ],
    data() {
        return {
            visible: "",
            title: "本周质量工作总结",
            isLock: false,
            btnStatus: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            currntData: null,
            dataInfo: null,
            startDates: null,
            formData: {
                fProjectid: "", //当前登录人项目ID
                fProjectname: "", //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
                fProjectfullname: "",
                fCompanyid: "", //左侧选中组织机构id
                fCompanyname: "", //当前登录人标段名称
                fCompanyfullname: "",
                fId: "", //提交后不允许修改与删除
                fCheckunit: "", //网格责任人单位
                fCheckunitid: "", //网格责任人单位id
                fQualitygridclass: "", //质量网格类别
                fDuty: "", //职务
                fChargeusername: "", //责任人姓名
                fChargeuserid: "", //责任人id
                fWeekreport: "", //填报周次
                fWeekdutydetail: "", //本周质量工作
                fFileurl: "", //相关资料附件
                userId: "", //当前登录人id
                userName: "", //当前登录人姓名
                fReportdates: this.common.getDay(0), //填报日期
                fWeekdates: this.common.getDay(0), //周总结日期
                recordfiles: [] //删除文件id集合
            },
            formDatafileList: [], //工作计划资料
            btnShow: true,
            currentStatus: null,
        };
    },
    components: {
        publicForm,
    },
    watch: {
        dialogVisible: {
            handler(newVal) {
                if (newVal) {
                    this.visible = newVal;
                }
            },
            immediate: true,
        },
        dialogTitle: {
            handler(newVal) {
                if (newVal) {
                    this.title = newVal;
                }
            },
            immediate: true,
        },
        projectId: {
            handler(newVal) {
                if (newVal) {
                    console.log(787, newVal);
                }
            },
            immediate: true,
        },
        status: {
            handler(newVal) {
                if (newVal) {
                    this.btnStatus = newVal;
                }
            },
            immediate: true,
        },
        statusNum: {
            handler(newVal) {
                if (newVal) {
                    this.edit(newVal);
                }
            },
            immediate: true,
        },
        dataFid: {
            handler(newVal) {
                if (newVal) {
                    this.currntData = newVal;
                    if (this.statusNum != 1) {
                        this.formData = this.$options.data.call(this).formData;
                        this.getDetail({
                            fId: newVal,
                        });
                    }
                }
            },
            immediate: true,
        },
        currentRow: {
            handler(newVal) {
                if (newVal) {
                    this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
        fCompanyid: {
            handler(newVal) {
                if (newVal) {
                    // this.currentStatus = newVal;
                }
            },
            immediate: true,
        },
    },
    created() {
        window.addEventListener("resize", this.getHeight);
        this.getHeight();
    }, //获取页面高度自适应：

    destroyed() {
        window.removeEventListener("resize", this.getHeight);
        this.getHeight();
    },
    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
        // this.getUserCompanyTreeListXm({
        //     companyId: this.userInfo.companyId,
        //     userId: this.userInfo.userId
        // })
        if (this.statusNum == 1) {
            this.btnShow = true
            this.getDetail({
                userId: this.userInfo.userId,
                fCompanyid: this.fCompanyid
            });
            this.formData = this.$options.data.call(this).formData;
            this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[4].disabled = false;
            this.formObj[6].disabled = false;
            this.formObj[8].uploadSelect = true;
        } else if (this.statusNum == 2) { //编辑
            //编辑
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[4].disabled = false;
            this.formObj[6].disabled = false;
            this.formObj[8].uploadSelect = true;
            this.btnShow = true
        } else if (this.statusNum == 7) { //查看
            this.btnShow = false
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[8].uploadSelect = false;
        }

        this.getHeight();
        // this.edit()
    },
    methods: {
        getHeight() {
            var h = document.documentElement.clientHeight; //获取页面可见高度
            this.dialogHeight = h * 0.76 + "px";
        },
        handleSelect(key) {
            this.activeIndex = key;
        },
        closeDialog() {
            this.$emit("close");
        },

        selectChange(data, a, b) {
            if (b == "fWeekdates") {
                this.getWeeks(this.formData.fWeekdates)
            }
        },
        Ispost(data) { //是否待岗
            getfChargeuseridData(data).then(res => {
                if (res.errcode == 0) {
                    var obj = res.data[0]
                    this.formData.fQualitygridclass = obj.fQualitygridclass //质量网格类别
                    this.formData.fChargeuserid = obj.fChargeuserid //责任人id
                    this.formData.fChargeusername = obj.fChargeusername //责任人姓名
                    this.formData.fDuty = obj.fDuty //职务
                    this.formData.fQualitydutydetail = obj.fQualitydutydetail
                }
            })
        },
        // 巡查项目部
        // getUserCompanyTreeListXm(data) {
        //     getUserCompanyTreeListXm(data).then(res => {
        //         if (res.errcode == 0) {
        //             this.formObj[2].options = []
        //             res.data.userCompanyTreeList.map(item => {
        //                 this.formObj[2].options.push({
        //                     label: item.fShortname,
        //                     value: item.fCompanyid
        //                 })

        //             })

        //         }
        //     })
        // },
        edit(status) {
            if (status == 1) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 2) {
                this.formData = this.$options.data.call(this).formData;
            } else if (status == 4) {
                this.monthExamineData = this.$options.data.call(this).monthExamineData;
            }
        },
        // 详情
        getDetail(data1) {
            getWeekdutyDetailByFid(data1).then((res) => {
                if (res.errcode == -2) { //当前质量未录入信息，当前
                    this.btnShow = false
                    this.$message.warning(res.msg);
                } else if (res.errcode == 0) {
                    if (this.statusNum == 1) {
                        this.btnShow = true;
                    } else if (this.statusNum == 2) {
                        this.btnShow = true;
                    } else if (this.statusNum == 7) {
                        this.btnShow = false;
                    }
                    var data = res.data[0];
                    if (data.fId != '') {
                        this.formData = data;
                        this.formDatafileList = data.fFileurllist;
                        console.log(this.formDatafileList );
                        this.formData.recordfiles = [];
                        let curDate = (new Date()).getTime();
                        let three = this.getDaysBetween(new Date(data.startDates), new Date(this.common.getDay(0))) * 24 * 3600 * 1000;
                        this.startDates = curDate - three;
                    } else {

                        this.userInfo = JSON.parse(sessionStorage.getItem("userInfo"));
                        this.formData.recordfiles = []
                        this.formData.fCheckunit = this.userInfo.companyName; ////网格责任人单位
                        this.formData.fCheckunitid = this.userInfo.companyId; //网格责任人单位id
                        this.formData.fQualitygridclass = data.fQualitygridclass; //质量网格类别
                        this.formData.fDuty = data.fDuty; //职务
                        this.formData.fChargeusername = data.fChargeusername //责任人姓名
                        this.formData.fChargeuserid = data.fChargeuserid //责任人姓名
                        this.formData.fWeekreport = data.fWeekreport //填报周次
                        this.formData.fFileurl = data.fFileurl //相关资料附件
                        this.formData.userId = this.userInfo.userId; //当前登录人id
                        this.formData.userName = this.userInfo.realName; //当前登录人名称
                        this.formData.fReportdates = this.common.getDay(0) //填报日期
                        this.formData.fWeekdates = this.common.getDay(0) //周总结日期
                        this.formData.fCompanyname = this.userInfo.companyName; //当前登录人标段名称
                        this.formData.fCompanyid = this.userInfo.companyId; //当前登录人标段ID
                        this.formData.fCompanyfullname = this.userInfo.companyName; //当前登录人标段名称
                        this.formData.fProjectfullname = this.projectName; //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
                        this.formData.fProjectname = this.projectName; //当前登录人项目名称(如果是项目部的保存接口,传这个人的所在上级的项目id)
                        this.formData.fProjectid = this.projectId; //当前登录人项目ID
                        let curDate = (new Date()).getTime();
                        let three = this.getDaysBetween(new Date(data.startDates), new Date(this.common.getDay(0))) * 24 * 3600 * 1000;
                        this.startDates = curDate - three;
                    }
                }
            });
        },
        getDaysBetween(date1, date2) {
            const ONE_DAY = 1000 * 60 * 60 * 24; // 一天的毫秒数
            const date1Time = date1.getTime(); // 获取时间戳
            const date2Time = date2.getTime();
            const difference = Math.abs(date1Time - date2Time); // 获取时间差
            return Math.round(difference / ONE_DAY); // 两个日期之间的天数
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.formDatafileList.length; index++) {
                if (file.uid == this.formDatafileList[index].uid) {
                    if (this.formDatafileList[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.formDatafileList[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.formDatafileList[index].atta.fId,
                        });
                    }
                    this.formDatafileList.splice(index, 1);
                }
            }
        },
        yj_saveForm() {
            if (this.statusNum == 1) { //新增
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            } else if (this.statusNum == 2) { //编辑
                if (this.$refs.publicForm.submitForm()) {
                    this.saveForm(this.formData);
                } else {
                    console.log("error");
                }
            }
        },
        getWeeks(data) {
            getWeeks({
                fCompanyid: this.userInfo.companyId,
                startDates: data
            }).then(res => {
                if (res.errcode == 'ok') {
                    this.formData.fWeekreport = res.msg
                }
            })
        },
        // 保存
        saveForm(data) {
            this.isLock = true
            saveWeekdutyDetail(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.isLock = false
                        this.visible = false;
                        this.$emit("refreshList");
                        this.$message.success("新增成功");
                    } else {
                        this.isLock = false
                        this.$message.error(res.msg);
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });

        },

    },
};